package queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: qian
 * Date: 2022-07-05
 * Time: 18:42
 */
public class MyQueue {
    class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val=val;
        }
    }
    public ListNode head;
    public ListNode last;
    public int usedSize;
    public void offer(int val){
        ListNode listNode=new ListNode(val);
        if(this.head==null){
            this.head=listNode;
            this.last=listNode;
        }else{
            listNode.next=this.head;
            this.head=listNode;
        }
        this.usedSize++;
    }
    public int poll(){
        int num=last.val;
        ListNode cur=this.head;
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }else if(this.head==this.last){
            this.head=null;
            this.last=null;
        }else{
            while(cur.next!=this.last){
                cur=cur.next;
            }
            cur.next=null;
            this.last=cur;
        }
        this.usedSize--;
        return num;
    }
    public boolean isEmpty(){
        return this.usedSize==0;
    }
    public int peek(){
        return this.last.val;
    }
    public int size(){
        return usedSize;
    }

}
